package com.ruoyi.technicianAudit.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.technicianAudit.domain.Recruitment;
import com.ruoyi.technicianAudit.service.IRecruitmentService;
import com.ruoyi.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 技师申请Controller
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/recruitment/recruitment")
public class RecruitmentController extends BaseController {
    @Autowired
    private IRecruitmentService recruitmentService;
    @Resource
    private UserMapper userMapper;

    /**
     * 查询技师申请列表
     */
    @PreAuthorize("@ss.hasPermi('recruitment:recruitment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Recruitment recruitment) {
        startPage();
        List<Recruitment> list = recruitmentService.selectRecruitmentList(recruitment);
        if (!CheckEmptyUtil.isEmpty(list)){
            for (Recruitment recruitment1 : list) {
                String userName = userMapper.selectUserName(recruitment1.getUserId());
                recruitment1.setUserName(CheckEmptyUtil.isEmpty(userName)?"":userName);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出技师申请列表
     */
    @PreAuthorize("@ss.hasPermi('recruitment:recruitment:export')")
    @Log(title = "技师申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Recruitment recruitment) {
        List<Recruitment> list = recruitmentService.selectRecruitmentList(recruitment);
        if (!CheckEmptyUtil.isEmpty(list)){
            for (Recruitment recruitment1 : list) {
                String userName = userMapper.selectUserName(recruitment1.getUserId());
                recruitment1.setUserName(CheckEmptyUtil.isEmpty(userName)?"":userName);
            }
        }
        ExcelUtil<Recruitment> util = new ExcelUtil<Recruitment>(Recruitment.class);
        util.exportExcel(response, list, "技师申请数据");
    }

    /**
     * 获取技师申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('recruitment:recruitment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(recruitmentService.selectRecruitmentById(id));
    }

    /**
     * 新增技师申请
     */
    @PreAuthorize("@ss.hasPermi('recruitment:recruitment:add')")
    @Log(title = "技师申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Recruitment recruitment) {
        return toAjax(recruitmentService.insertRecruitment(recruitment));
    }

    /**
     * 修改技师申请
     */
    @RepeatSubmit(interval = 1000, message = "正在提交，请勿重复提交")
    @PreAuthorize("@ss.hasPermi('recruitment:recruitment:edit')")
    @Log(title = "技师申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Recruitment recruitment) {
        return toAjax(recruitmentService.updateRecruitment(recruitment));
    }

    /**
     * 删除技师申请
     */
    @PreAuthorize("@ss.hasPermi('recruitment:recruitment:remove')")
    @Log(title = "技师申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(recruitmentService.deleteRecruitmentByIds(ids));
    }
}
